package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.LinkedList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Account;
import model.Connect;
import model.SQLModel;
import model.Table;
import model.User;



/**
 * Servlet implementation class ServletLogin
 */
public class SubmitObjectAudit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SubmitObjectAudit() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		try{

			if (request.getMethod().equalsIgnoreCase("post")||request.getMethod().equalsIgnoreCase("get")) {
				try {
					Connect connect = new Connect();
					Account account=connect.checkSession(request, response);
					if(!"admin_role".equalsIgnoreCase(account.strRole)){
						//send error
					}	
					connect.setSID(connect.getdbSession(request));
					connect.setUser(account.strUserName);
					connect.setPassword(account.strPass);
					Connection conn = connect.simple_connect();
					String owner=(String)request.getParameter("owner");
					String obj=(String)request.getParameter("obj");
					String granter=account.strUserName;
					SQLModel db = new SQLModel(conn);
					//selectsc
					//LinkedList<Sales> lst= db.select();
					boolean isAudited = db.isAuditWhole();
				    LinkedList<User>lst=db.getListAuditUsers(granter, owner, obj);
				    LinkedList<Table>lst1;
				    RequestDispatcher dispatcher;
				    if(!granter.equals("ADMIN")){
				    	lst1=db.getAllTablesOfUser(granter);
				    	  dispatcher= getServletContext().getRequestDispatcher("/JSP/userView/auditManagement.jsp");
				    }
				    else {
				    	lst1= db.getAccessTablesOfUser("ADMIN");
				    	  dispatcher= getServletContext().getRequestDispatcher("/JSP/adminView/objectAuditing.jsp");
				    }
				    
				    for(int i =0; i<lst.size();i++){
				    	User u = lst.get(i);
				    	String tmp ="";
				    	if(request.getParameter("select"+i)!=null)tmp=tmp+"SELECT,";
				    	if(request.getParameter("insert"+i) != null)tmp=tmp+"INSERT,";
				    	if(request.getParameter("update"+i) != null)tmp=tmp+"UPDATE,";
				    	if(request.getParameter("delete"+i) != null)tmp=tmp+"DELETE,";
				    	if(tmp.endsWith(",")){tmp=tmp.substring(0,tmp.length()-1);}
				    	System.out.println("New String" +tmp +"vs OLD String"+u.auditType  );
				    	if(tmp.equals("")&&!u.auditType.equals("")){
				    		db.dropFGAPolicy(granter, u.username, owner, obj);
				    		u.auditType=tmp;
				    	}
				    	else if(!tmp.equals("")&&u.auditType.equals("")){
				    		System.out.println(granter+u.username+owner+obj+tmp);
				    		db.createFGAPolicy(granter,  u.username, owner, obj,tmp);
				    		u.auditType=tmp;
				    	}
				    	else if(!tmp.equalsIgnoreCase(u.auditType)){
				    		db.dropFGAPolicy(granter, u.username, owner, obj);
				    		db.createFGAPolicy(granter,  u.username, owner, obj,tmp);
				    		u.auditType=tmp;
				    	}
				    	
				    	
				    }
				    
				    conn.close();
					request.setAttribute("lstUser", lst);
					request.setAttribute("lstTable", lst1);
					request.setAttribute("owner",owner);
					request.setAttribute("obj",obj);
					request.setAttribute("isAudited", isAudited);
					dispatcher.forward(request,response);
					

				} catch (Exception e) {
					System.out.print("Connection unsuccessfly "+ e.getMessage());
					//response.sendRedirect("ManageAccount/Login.jsp");
				}
			}

		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

}
